<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use app\admin\model\Commons;
use app\admin\model\Qinu;
use app\admin\model\Upfiles;
use think\Db;
use think\Validate;
use app\admin\model\Auths;
use think\Request;

class Members
{
//    public function __construct(Request $request)
//    {
//        Auths::login_auth($request);
//    }

    /**加载添加成员页面
     * @return \think\response\View
     */
    public function show_add(Request $request){
        $model = $request->param('model');
        $pid = $request->param('pid',null);
        if($pid){
            $lanmus = (new Commons())->getData($pid);
            $lanmu = [];
            $docu   = Db::name('document')->where('id',$pid)->find();
            $lanmus[] = $docu;
            $modeid = (Db::name('model')->where('surface','cate')->find())['id'];
            foreach ($lanmus as $value){
                if($value['model']==$modeid){
                    $lanmu[] = $value;
                }
            }
        }else{
            $lanmu = Db::view('document',true)
                ->view('model','surface','document.model=model.id','left')
                ->where('surface','cate')
                ->select();
        }

        return view('members_add',['model'=>$model,'lanmu'=>$lanmu,'pid'=>$pid]);
    }

    /**添加团队成员的逻辑
     * @return int|string
     */
    public function members_add(Request $request){
        $data = $request->param();
        //验证是否符合
        $num = val($data,[
            'title'=>'require',
            'pid'  =>'require',
            'model'=>'require',
            'status'=>'require',
            'sort'  =>'require',
        ]);
        if($num!==1){
            return $num;
        }
        //讲document表数据单独取出
        $document['title'] = $data['title'];
        $document['pid'] = $data['pid'];
        $document['model'] = $data['model'];
        $document['status'] = $data['status'];
        $document['dir'] = $data['dir'];
        $document['sort'] = $data['sort'];
        //删除$data的多余数据
        unset($data['title']);
        unset($data['pid']);
        unset($data['model']);
        unset($data['status']);
        unset($data['dir']);
        unset($data['sort']);
        $images = Qinu::Upfile($_FILES['images']);
        if($images){
            $data['images'] = $images;
        }
        //获取uid
        $uid = Db::name('document')->insertGetId($document);
        $data['uid'] = $uid;
        if(Db::name('members')->insert($data)){
            return 1;
        }else{
            return '添加失败';
        }
    }

    /**加载编辑成员页面
     * @return string|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(Request $request){
        $uid = $request->param('uid');
        $pid = $request->param('pid',null);
        $data = Db::view('document',true)
            ->view('model','surface','model.id=document.model','left')
            ->view('members','*','document.id=members.uid','left')
            ->where('document.id',$uid)
            ->find();
        $lanmu = Db::view('document',true)
            ->view('model','surface','model.id=document.model','left')
            ->where('surface','cate')
            ->select();
//        echo '<pre>';
//        print_r($data);exit;
        return view('members_edit',['data'=>$data,'lanmu'=>$lanmu,'pid'=>$pid]);
    }

    /**更新
     * @return array|int|string
     */
    public function members_update(Request $request){
        $data = $request->param();
        //验证是否符合
        $num = val($data,[
            'title'=>'require',
            'pid'  =>'require',
            'status'=>'require',
            'sort'  =>'require',
            'uid'   =>'require'
        ]);
        if($num!==1){
            return $num;
        }
        //讲document表数据单独取出
        $document['title'] = $data['title'];
        $document['pid'] = $data['pid'];
        $document['status'] = $data['status'];
        $document['dir'] = $data['dir'];
        $document['sort'] = $data['sort'];
        //删除$data的多余数据
        unset($data['title']);
        unset($data['pid']);
        unset($data['status']);
        unset($data['dir']);
        unset($data['sort']);
        //保存数据到文档表（document）
        //Db::name('document')->where('id',$data['uid'])->update($document);
        $images = Qinu::Upfile($_FILES['images']);
//        return $images;
        $resul = Db::connect();
        //如果更新了图标则删除原来的图标
        if($images){
            $members = $resul->name('members')->where('uid',$data['uid'])->find();
            if($members['images']){
                Qinu::DeleFile($members['images']);
            }
            $data['images'] = $images;
        }
        //更新数据
        if($resul->name('document')->where('id',$data['uid'])->update($document)||$resul->name('members')->where('uid',$data['uid'])->update($data)){
            return 1;
        }else{
            return '更新失败';
        }
    }
}